package drawingBoard.myFile;

import drawingBoard.myIterator.ConcreteAggregate;
import drawingBoard.myIterator.Iterator;
import drawingBoard.myIterator.MyList;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/*
 * ImageFileChooser作为 javax.swing.JFileChooser（文件选择器）的子类
 * ImageFileChooser依赖类 javax.swing.filechooser.FileFilter
 */
public class ImageFileChooser extends JFileChooser {  //JFileChooser的子类
	private static final long serialVersionUID = 1L;
	public ImageFileChooser() {  //无参构造方法
		super();
		setAcceptAllFileFilterUsed(false);  //AllFile
		addFilter();  //文件类型过滤
	}
	public ImageFileChooser(String currentDirectoryPath) {   //有参构造方法
		super(currentDirectoryPath);
		setAcceptAllFileFilterUsed(false);
		addFilter();
	}
	public String getSuf(){  //获取后缀名
		FileFilter fileFilter=getFileFilter();  //获取文件过滤对象
		String desc=fileFilter.getDescription();  //System.out.println(desc);
		String[] sufarr = desc.split(" ");
		String suf=sufarr[0].equals("所有图形文件")?"":sufarr[0];
		return suf.toLowerCase();
	}
	private void addFilter(){  //增加文件过滤器，私有方法
		MyList myList=new ConcreteAggregate();
		myList.add(new MyFileFilter(new String[] { ".BMP" }, "BMP (*.BMP)"));
		myList.add(new MyFileFilter(new String[] { ".GIF" }, "GIF (*.GIF)"));
		myList.add(new MyFileFilter(new String[] { ".TIF", ".TIFF" }, "TIFF (*.TIF;*.TIFF)"));
		myList.add(new MyFileFilter(new String[] { ".PNG" }, "PNG (*.PNG)"));
		myList.add(new MyFileFilter(new String[] { ".ICO" }, "ICO (*.ICO)"));
		myList.add(new MyFileFilter(new String[] { ".JPG", ".JPEG", ".JPE", ".JFIF" },"JPEG (*.JPG;*.JPEG;*.JPE;*.JFIF)"));
		myList.add(new MyFileFilter(new String[]{".BMP",".JPG",".JPEG",".JPE",".JFIF",".GIF",".TIF",".TIFF",".PNG",".ICO"},"所有图形文件"));
		Iterator it=myList.iterator();
		while(it.hasNext()){
			addChoosableFileFilter((MyFileFilter)it.next()); //添加可选择的文件过滤器
		}
	}
}
